﻿<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Reference Counting">
<meta name="DC.subject" content="Reference Counting">
<meta name="keywords" content="Reference Counting">
<meta name="DC.Relation" scheme="URI" content="../../tbb_userguide/Design_Patterns/Design_Patterns.htm">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="Reference_Counting">
<link rel="stylesheet" type="text/css" href="../../intel_css_styles.css">
<title>Reference Counting</title>
<xml>
<MSHelp:Attr Name="DocSet" Value="Intel"></MSHelp:Attr>
<MSHelp:Attr Name="Locale" Value="kbEnglish"></MSHelp:Attr>
<MSHelp:Attr Name="TopicType" Value="kbReference"></MSHelp:Attr>
</xml>
</head>
<body id="Reference_Counting">
 <!-- ==============(Start:NavScript)================= -->
 <script src="..\..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
 <script language="JavaScript1.2" type="text/javascript">WriteNavLink(2);</script>
 <!-- ==============(End:NavScript)================= -->
<a name="Reference_Counting"><!-- --></a>

 
  <h1 class="topictitle1">Reference Counting</h1>
 
   
  <div> 
	 <div class="section"><h2 class="sectiontitle">Problem</h2> 
		 
		<p>Destroy an object when it will no longer be used. 
		</p>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">Context</h2> 
		 
		<p>Often it is desirable to destroy an object when it is known that it
		  will not be used in the future. Reference counting is a common serial solution
		  that extends to parallel programming if done carefully. 
		</p>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">Forces</h2> 
		 
		<ul type="disc"> 
		  <li> 
			 <p>If there are cycles of references, basic reference counting is
				insufficient unless the cycle is explicitly broken. 
			 </p>
 
		  </li>
 
		  <li> 
			 <p>Atomic counting is relatively expensive in hardware. 
			 </p>
 
		  </li>
 
		</ul>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">Solution</h2> 
		 
		<p>Thread-safe reference counting is like serial reference counting,
		  except that the increment/decrement is done atomically, and the decrement and
		  test "count is zero?" must act as a single atomic operation. The
		  following example uses 
		  <samp class="codeph">tbb::atomic&lt;int&gt;</samp> to achieve this. 
		</p>
 
		<pre>template&lt;typename T&gt;
class counted {
   tbb::atomic&lt;int&gt; my_count;
   T value;
public:
   // Construct object with a single reference to it.
   counted() {my_count=1;}
   // Add reference
   void add_ref() {++my_count;}
   // Remove reference. Return true if it was the last reference.
   bool remove_ref() {return ‐‐my_count==0;}
   // Get reference to underlying object
   T&amp; get() {
       assert(my_count&gt;0);
       return my_value;
   }
};</pre> 
		<p>It is incorrect to use a separate read for testing if the count is
		  zero. The following code would be an incorrect implementation of method 
		  <samp class="codeph">remove_ref</samp>() because two threads might both execute
		  the decrement, and then both read 
		  <samp class="codeph">my_count</samp> as zero. Hence two callers would both be told
		  incorrectly that they had removed the last reference. 
		</p>
 
		<pre>      ‐‐my_count;
      return my_count==0. <span style="color:blue"><strong>// WRONG!</strong></span></pre> 
		<p>The decrement may need to have a 
		  <em>release</em> fence so that any pending writes complete before the
		  object is deleted. 
		</p>
 
		<p>There is no simple way to atomically copy a pointer and increment its
		  reference count, because there will be a timing hole between the copying and
		  the increment where the reference count is too low, and thus another thread
		  might decrement the count to zero and delete the object. Two ways to address
		  the problem are "hazard pointers" and "pass the buck". See the references below
		  for details. 
		</p>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">Variations</h2> 
		 
		<p>Atomic increment/decrement can be more than an order of magnitude more
		  expensive than ordinary increment/decrement. The serial optimization of
		  eliminating redundant increment/decrement operations becomes more important
		  with atomic reference counts. 
		</p>
 
		<p>Weighted reference counting can be used to reduce costs if the
		  pointers are unshared but the referent is shared. Associate a 
		  <em>weight</em> with each pointer. The reference count is the sum of the
		  weights. A pointer 
		  <samp class="codeph">x</samp> can be copied as a pointer 
		  <samp class="codeph">x</samp>' without updating the reference count by splitting
		  the original weight between x and x'. If the weight of x is too low to split,
		  then first add a constant W to the reference count and weight of x. 
		</p>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">References</h2> 
		 
		<p>D. Bacon and V.T. Rajan, "Concurrent Cycle Collection in Reference
		  Counted Systems" in 
		  <cite>Proc. European Conf. on Object-Oriented Programming</cite> (June
		  2001). Describes a garbage collector based on reference counting that does
		  collect cycles. 
		</p>
 
		<p>M. Michael, "Hazard Pointers: Safe Memory Reclamation for Lock-Free
		  Objects" in IEEE Transactions on Parallel and Distributed Systems (June 2004).
		  Describes the "hazard pointer" technique. 
		</p>
 
		<p>M. Herlihy, V. Luchangco, and M. Moir, "The Repeat Offender Problem: A
		  Mechanism for Supporting Dynamic-Sized, Lock-Free Data Structures" in 
		  <cite>Proceedings of the 16th International Symposium on Distributed
			 Computing</cite> (Oct. 2002). Describes the "pass the buck" technique. 
		</p>
 
	 </div>
 
  </div>
 

<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong>&nbsp;<a href="../../tbb_userguide/Design_Patterns/Design_Patterns.htm">Design Patterns</a></div>
</div>
<div></div>

</body>
</html>
